-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue implementation using stack (array).cpp
65 lines (56 loc) · 1.21 KB
/
Queue implementation using stack (array).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
using namespace std;
const int n = 5;
int A[n] = {0}, B[n] = {0}, front = -1, rear = front;
int full(){
return (rear >= n-1);
}
int empty(){
return (front == -1 || rear == -1);
}
void push(int data){
if(full()){
cout << "[QUEUE IS FULL]! Element [" << data << "] not pushed into queue." << endl;
} else {
rear = (front == -1) ? ++front : ++rear;
A[rear] = data;
}
}
void pop(){
if(empty()){
cout << "[QUEUE IS EMPTY]! No element popped." << endl;
} else if(front == rear){
front = rear = -1;
} else {
int temp = -1;
while(!empty()){ // Pop-out from the array A to B
B[++temp] = A[rear--];
}
temp--; // Pop-out from the B
while(temp >= 0){ // Pop-out elements from the array B to A
A[++rear] = B[temp--];
}
}
}
void print(){
if(empty()){
cout << "[EMPTY QUEUE]" << endl;
} else {
while(!empty()){
cout << A[front];
pop();
if(!empty()) cout << " <= ";
}
cout << endl;
}
}
int main(){
push(5);
push(10);
push(15);
push(20);
push(25);
pop();
pop();
print();
}